package com.chengzzz.mysite.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chengzzz.mysite.dao.AnimationDao;
import com.chengzzz.mysite.dao.UserDao;
import com.chengzzz.mysite.entity.Animation;
import com.chengzzz.mysite.entity.Source;
import com.chengzzz.mysite.entity.User;
import com.chengzzz.mysite.enums.ResultEnum;
import com.chengzzz.mysite.pojo.UserPojo;
import com.chengzzz.mysite.result.Result;
import com.chengzzz.mysite.service.AnimationService;
import com.chengzzz.mysite.service.SourceService;
import com.chengzzz.mysite.service.UserService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLOutput;
import java.util.*;

/**
 * (User)表服务实现类
 *
 * @author 等什么柠檬君
 * @since 2020-08-24 14:26:50
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private SourceService sourceService;

    @Autowired
    private AnimationService animationService;

    @Autowired
    private AnimationDao animationDao;

    @Override
    public Result userValidation(UserPojo userPojo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        Map map = new HashMap();
        map.put("email", userPojo.getEmail());
        map.put("password", userPojo.getPassword());
        queryWrapper.allEq(map);
        User user = userDao.selectOne(queryWrapper);
        if (null != user) {
            // List list =  animationDao.selectMulti();
            if (!user.getRegisteip().equals(userPojo.getLoginip())) {
                return new Result(ResultEnum.LOGIN_SUCCESS.getCode(), "登陆成功,您当前的ip非注册ip可能存在风险", null, user);
            } else {
                return new Result(ResultEnum.LOGIN_SUCCESS.getCode(), "登陆成功。欢迎，" + user.getUsername(), null, user);
            }
        } else {

            return new Result(ResultEnum.PASSWORD_ERROE.getCode(), "登陆失败,请检查你的账号及密码");
        }

    }



    public Boolean userIsExist(User user) {
        QueryWrapper queryWrapper = new QueryWrapper();
        Map map = new Hashtable();
        map.put("email", user.getEmail());
        map.put("password", user.getPassword());
        queryWrapper.allEq(map);
        int  userDB = userDao.selectCount(queryWrapper);
        if (userDB == 1 ){
            return true;
        }
        return false;

    }

    @Override
    public User userIdentValidation(User user) {
        QueryWrapper queryWrapper = new QueryWrapper();
        Map map = new Hashtable();
        map.put("email", user.getEmail());
        map.put("password", user.getPassword());
        queryWrapper.allEq(map);
        userDao.update(user,queryWrapper);
        User userDB = userDao.selectOne(queryWrapper);
        return userDB;
    }

    @Override
    public User findUserByEmail(String email) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("email",email);
        return userDao.selectOne(queryWrapper);
    }



    @Override
    public Result UserRegist(User user) {
        user.setUsername("韭菜");
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("email", user.getEmail());
        if (userDao.selectOne(wrapper) != null) {
            return new Result(ResultEnum.EAMILERROR.getCode(), "该邮箱已被注册", null, false);
        } else {

            if (userDao.insert(user) != 0) {

                return new Result(ResultEnum.REGISTSUCCESS.getCode(), "注册成功", null, true);
            } else {

                return new Result(ResultEnum.EAMILERROR.getCode(), "注册失败", null, false);
            }

        }


    }

    @Override
    public Result UserDelete(Long uuid) {
   int num = userDao.deleteById(uuid);
        if (num != 0) {
            return new Result(ResultEnum.DELETESUCESS.getCode(), "删除成功", "数量："+num, true);
        }
        return new Result(ResultEnum.DELETEERROR.getCode(), "删除失败", "数量："+num, false);
    }

    @Override
    public IPage<User> ListAllUser(Integer page, Integer size) {
        Page<User> userPage = new Page<>(page,size);
        return userDao.selectPage(userPage,null);
    }

    @Override
    public Result UserUpdate(User user) {
        int num = userDao.updateById(user);
        if (num != 0) {
            return new Result(ResultEnum.SUCCESS.getCode(), "更新成功", "数量：" + num, true);
        }
        return new Result(ResultEnum.ERROR.getCode(), "更新失败", "数量：" + num, false);
    }

    @Override
    public Result insertUser(User user) {
        System.out.println(user.getEmail());
        System.out.println(user.getUsername());
        QueryWrapper queryWrapper = new QueryWrapper();
        Map map = new Hashtable();
        map.put("email", user.getEmail());
        map.put("password", user.getPassword());
        queryWrapper.allEq(map);
        if (userDao.selectOne(queryWrapper) != null) {
            return new Result(ResultEnum.ERROR.getCode(), "插入失败，此用户已存在", "数量：0", false);
        } else {
            if (userDao.insert(user) != 0) {
                return new Result(ResultEnum.SUCCESS.getCode(), "插入成功", "数量：", true);
            } else {
                return new Result(ResultEnum.ERROR.getCode(), "插入失败", "数量：0", false);
            }
        }
    }
}